
chrome.runtime.onMessage.addListener(  
  function(request, sender, sendResponse) {  
    console.log('background message listener, type: ' + request.type)
    if (request.type == "launch"){  
        connectToNativeHost(request.message);  
    }  
    return true;  
});  

var port = null;
var nativeHostName = "com.exe.name";

//connect to native host and get the communicatetion port  
function connectToNativeHost(msg)  
{  
    port = chrome.runtime.connectNative(nativeHostName); 

    // listen native exe message
    port.onMessage.addListener(function (message) {
        console.log('recieved message from native exe: ' + JSON.stringify(message));
    });

    // listen native exe disconnect
    port.onDisconnect.addListener(function () {
        console.log('disconnected from native app.'); 
        console.log(chrome.runtime.lastError);   
        port = null; 
    });  

    // send message
    port.postMessage(msg);

 }